const api = require("../../../utils/marketAPIUtils");
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    order:{}
  },

  _loadInfoData:function(tradeNo){
      wx.showLoading({
        title: '加载中...',
      })
    var self = this;
    api.post(api.url.getMarketOrder,{
        tradeNo:tradeNo
    },function(res){
        wx.hideLoading();
        console.log(res.data);
        if(res.data.code == 200){
          self.setData({
            order:res.data.body
          })
        }
    });
  },

  //关闭订单
  btnCloseOrder:function (e) {
    let tradeNo = this.data.order.tradeNo;
    let createDate = this.data.order.createDate;
    const _self = this;
    wx.showModal({
      title: '确定要关闭订单吗？请问什么原因',
      editable:true,
      complete: (res) => {
        if (res.confirm) {
          if(!res.content){
              wx.showToast({
                  title: '请填写原因',
                  icon:'error'
              })
              return;
          }
          console.log(res.content);
          api.post(api.url.closeMarketOrder,{
              tradeNo:tradeNo,
              createDate:createDate,
              closeReason:res.content
          },function(res){
              console.log(res.data);
              if(res.data.code == 200){
                _self._loadInfoData(tradeNo);
              }else if(res.data.msg){
                  wx.showModal({
                    title: '温馨提示',
                    content: res.data.msg,
                    showCancel:false
                  })
              }else{
                  wx.showToast({
                      title: '关闭订单失败',
                      icon:'error'
                  })
              }
          });
        }
      }
    })
  },

  btnPay:function(){
      wx.showLoading({
        title: '处理中...',
      })
    var _self = this;
    api.post(api.url.goodsPay,{
      tradeNo:this.data.order.tradeNo,
      createDate:this.data.order.createDate,
      appId:wx.getAccountInfoSync().miniProgram.appId,
      payType:3
    },function(res){
      wx.hideLoading();
      console.log(res.data);
      if(res.data.code == 200){
        var payInfo = JSON.parse(res.data.body.payInfo);
        wx.requestPayment({
          appId: payInfo.appId,
          timeStamp: payInfo.timeStamp + "",
          nonceStr: payInfo.nonceStr,
          package: payInfo.package,
          signType: payInfo.signType,
          paySign: payInfo.sign,
          success(res) {
            wx.showModal({
                title: '温馨提示',
                content: '支付成功',
                showCancel: false,
                complete:function(params) {
                    _self._loadInfoData(tradeNo);
                }
            })
          }
          ,fail(res) {
            console.log(res);
          }
        });
      }else if(res.data.msg){
        wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            showCancel: false
        })
      }else{
          wx.showToast({
            title: '支付失败',
            icon:'error'
          })
      }
    });
  },

  btnReceive(){
    var self = this;
    var tradeNo = this.data.order.tradeNo;
    wx.showModal({
        title: '温馨提示',
        content:'确定收货吗？',
        success:function(res){
            if (res.confirm) {
                wx.showLoading({
                  title: '处理中...'
                })
                api.post(api.url.receiveMarketOrder,{
                    tradeNo:tradeNo
                    },function(res){
                        wx.hideLoading();
                        console.log(res.data);
                        if(res.data.code == 200){
                            wx.showModal({
                                title: '温馨提示',
                                content: '收货成功',
                                showCancel: false,
                                complete:function(params) {
                                    self._loadInfoData(tradeNo);
                                }
                            })
                        }else if(res.data.msg){
                            wx.showModal({
                                title: '温馨提示',
                                content: res.data.msg,
                                showCancel: false
                            })
                        }else{
                            wx.showToast({
                                title: '订单收货失败',
                                icon:'error'
                            })
                        }
                });
            }
        }
    })
  },

  /**
   * 切换收货地址
   */
  toChangeAddress:function(){
    try {
        wx.removeStorageSync(app.globalData.ks_buyAddress);
    } catch (e) { }
    var id = this.data.order.mailAddress.id;
    var tradeNo = this.data.order.tradeNo;
    wx.navigateTo({
      url: './address?id='+id+'&tradeNo='+tradeNo
    })
  },

  toIndex(){
      wx.reLaunch({
        url: '../index/index'
      })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
      if(options && options.tradeNo){
        let tradeNo = options.tradeNo;
        this._loadInfoData(tradeNo)
      }else{
        wx.showToast({
            title: '参数错误',
            icon:'error'
        })
        setTimeout(() => {
            wx.reLaunch({
                url: '../index/index',
            })
        }, 1000);
      }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})